package ru.btabank.dom.xpath.expression;

import java.util.ArrayList;

import ru.btabank.dom.xpath.XPathException;

public abstract class LocationPath extends Expression {
    private ArrayList steps;

    public LocationPath() {
        steps = new ArrayList(16);
    }

    public void addStep(Step step) {
        steps.add(step);
    }

    public int getStepsCount() {
        return steps.size();
    }

    public Step getStep(int position) {
        return (Step)steps.get(position);
    }

    public Object evaluate(NodeSet context) throws XPathException {
        return this.select(context);
    }

    public NodeSet select(NodeSet nodeSet) throws XPathException {
        for (int stepIndex = 0; stepIndex < this.getStepsCount(); stepIndex++) {
            nodeSet = this.getStep(stepIndex).select(nodeSet);
            if (nodeSet.getLength() == 0)
                break;
        }
        return nodeSet;
    }

    public NodeSet build(NodeSet nodeSet) throws XPathException {
        for (int stepIndex = 0; stepIndex < this.getStepsCount(); stepIndex++) {
            nodeSet = this.getStep(stepIndex).build(nodeSet);
            if (nodeSet.getLength() == 0)
                break;
        }
        return nodeSet;
    }
}
